﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web.Routing;

using ProjectBase.Core;
using ProjectBase.Core.Web;

namespace ProjectBase.Mvc
{
    public class AsyncComboView : DetailLinkControl<IPersistentObject, AsyncComboView>
    {
        #region members
        string _listActionUrl;
        bool _hasAsyncButton = true;
        #endregion members

        #region constructor
        public AsyncComboView() { }
        public AsyncComboView(bool? asReadonly = null) : base(asReadonly) { }
        public AsyncComboView(string prefix) : base(prefix) { }
        #endregion constructor

        #region OnSourceProperty
        protected override void OnSourcePropertyChanged(string propertyName, IPersistentObject value)
        {
            base.OnSourcePropertyChanged(propertyName, value);

            if (value.Is())
            {
                SetDetailEntityID(value.IDToDisplay);
                SetText(value.ToDisplay());
                SetAutoInitialText(value.ToDisplay());
            }
            BindProperty = propertyName;
        }
        #endregion OnSourceProperty

        #region Render
        protected override void ReadControls()
        {
            Controls.Add(CreateDiv());
        }

        protected override void WriteControls()
        {
            if (AutoCompleteAction.IsNotEmpty())
            {
                Controls.Add(CreateContent());
                if (HasAsyncButton)
                {
                    Controls.Add(CreateButton());
                }
            }
            else
            {
                HideLinkToDetail(true);
                Controls.Add
                    (
                        CreateDiv()
                            .AddControls
                            (
                                new Input(InputType.Hidden)
                                    .SetName(InputName)
                                    .SetValue(DetailEntityID),
                                HasAsyncButton ? CreateButton() : null
                            )
                    );
            }
        }

        protected virtual Div CreateDiv()
        {
            return new Div(GetAttribute(Attr.Class))
            {
                new Span
                {
                    new Anchor()
                        .SetTitle(Text.Length > MaxLength ? Text : null)
                        .SetTitle(Title)
                        .SetHref(HasDetailLink ? DetailUrl : null)
                        .SetCssClassName(HasDetailLink ? null : "none noPointer")
                        .AddCssClassName(GetAttribute(Attr.Class))
                        .AddControls
                        (
                            new Literal(Text.ToLength(MaxLength, true))
                        )
                }
            };
        }

        protected virtual VisualControl<ICoreModel> CreateContent()
        {
            return new PlaceHolder
            {
                new Input(InputType.Text)
                    .SetCssClassName("autoComplete")
                    .AddCssClassName(GetAttribute(Attr.Class))
                    .SetValue(AutoInitialText)
                    .SetAttribute("data-autocompleteurl", AutoCompleteUrl),
                new Input(InputType.Hidden)
                    .SetName(InputName)
                    .SetValue(DetailEntityID),
            };
        }

        protected virtual VisualControl<ICoreModel> CreateButton()
        {
            return new PlaceHolder
            {
                new Button(Special.ThreeDots)
                    .SetCssClassName("async")
                    .SetName("asyncComboView")
                    .SetAttribute("data-asyncurl", ListActionUrl),
            };
        }
        #endregion Render

        #region public Set()
        /// <summary>
        ///     The Item Property name to be filled by filter resut
        /// </summary>
        /// <param name = "propertyName"></param>
        /// <returns></returns>
        public virtual AsyncComboView SetResultBindProperty(string propertyName)
        {
            if (propertyName.IsNotEmpty())
            {
                BindProperty = propertyName;
            }
            return this;
        }

        public virtual AsyncComboView SetAutoCompleteAction(string autoCompleteAction)
        {
            if (autoCompleteAction.IsNotEmpty())
            {
                AutoCompleteAction = autoCompleteAction;
            }
            return this;
        }

        public virtual AsyncComboView SetListController(string controller)
        {
            if (controller.IsNotEmpty())
            {
                ListController = controller;
            }
            return this;
        }

        public virtual AsyncComboView SetListAction(string action)
        {
            if (action.IsNotEmpty())
            {
                ListAction = action;
            }
            return this;
        }

        public virtual AsyncComboView SetAutoInitialText(string initialText)
        {
            if (initialText.IsNotEmpty())
            {
                AutoInitialText = initialText;
            }
            return this;
        }
        public virtual AsyncComboView HideAsyncButton(bool hide)
        {
            HasAsyncButton = !hide;
            return this;
        }
        #endregion public Set()

        #region properties
        /// <summary>
        ///     The Item Property name to be filled by filter resut
        /// </summary>
        protected virtual string BindProperty { get; set; }

        public virtual string ListController { get; set; }

        public virtual bool HasAsyncButton
        {
            get { return _hasAsyncButton; }
            set { _hasAsyncButton = value; }
        }

        public virtual string ListAction { get; set; }
        public virtual string AutoInitialText { get; set; }
        public virtual string AutoCompleteAction { get; set; }

        protected virtual string AutoCompleteUrl
        {
            get
            {
                if (AutoCompleteAction.IsNotEmpty()
                    && (ListController.IsNotEmpty()
                        || DetailController.IsNotEmpty()))
                {
                    return Url.Action(
                        AutoCompleteAction,
                        DetailController.IsNotEmpty() ? DetailController : ListController,
                        CreateRouteValues());
                }
                return null;
            }
        }

        public virtual string ListActionUrl
        {
            get
            {
                if (_listActionUrl.IsEmpty())
                {
                    if (BindProperty.IsEmpty())
                    {
                        return string.Empty;
                    }
                    return Url.Action(
                        ListAction ?? Constants.Actions.List,
                        ListController ?? DetailController,
                        CreateRouteValues());
                }
                return _listActionUrl;
            }
            set { _listActionUrl = value; }
        }
        #endregion properties
    }
}